package com.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * @author: 一个小菜逼
 * @Date: 2020/5/1
 */
public class ImportantClass {

    public static void main(String[] args) {

//        CountDownLatch countDownLatch = new CountDownLatch(5);
//        countDownLatch.countDown();
//        try {
//            countDownLatch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }


//        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
//            System.out.println("执行任务的线程："+Thread.currentThread().getName());
//        });
//
//        for (int i = 0; i < 3; i++) {
//            new Thread(()->{
//                try {
//                    System.out.println("当前线程："+Thread.currentThread().getName());
//                    cyclicBarrier.await();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }).start();
//        }

        
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 4; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName());
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            }).start();
        }

    }

}
